﻿<%@ WebHandler Language="C#" Class="OrderHandler" %>

using System;
using System.Web;
using SeDuce.Model;
using SeDuce.BLL;
using System.Collections.Generic;

public class OrderHandler : IHttpHandler {

    Lazy_Yu<OrderBLL> lazyOrderBLL = new Lazy_Yu<OrderBLL>(() => new OrderBLL());
    public OrderBLL LazyOrderBLL
    {
        get { return this.lazyOrderBLL.Value; }
    }
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        OrderJSON orderJson = context.Request.Form["order"].ToString().FromJsonTo<OrderJSON>();

        List<string> sizes = new List<string>();
        orderJson.Sizes.ForEach(s=>{sizes.Add(s.Size);});
        List<string> colors = new List<string>();
        orderJson.Colors.ForEach(c => { colors.Add(c.Color); });
        
        string orderID = DateTime.Now.ToString("yyyyMMddHHmmssfff");
        bool result = LazyOrderBLL.InsertOrder(new Order() {
            OrderID = orderID,
            ProductID = orderJson.ProductID,
            Count = orderJson.Count,
            Phone = orderJson.Phone,
            QQ = orderJson.QQ,
            Email = orderJson.Email,
            Address = orderJson.Address,
            Memo = orderJson.Memo,
            Sizes = String.Join(",", sizes.ToArray()),
            Colors = String.Join(",", colors.ToArray()),
            Price = orderJson.Price,
            Date = DateTime.Now
        });
        context.Response.Write(new JSONStatus() { Status = result ? orderID : false.ToString() }.ToJSON());
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}